/*****************************************************************************
This program calculates the IK, CCFT, and CCFT w/ covs bandwidths
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

* Arguments
args grad_sep suffix

* Load input
use  "${cleandata}data_for_bw_ms_${year}`suffix'.dta", clear

tostring stu, replace

* Merge demographics
merge m:1 stu using "${cleandata}demo_info_05th_grade.dta", keep(1 3) gen(demo_merge) keepusing(bl_*)
destring bl_ell bl_swd, replace

* Merge Baseline scores (grade 5)
merge m:1 stu using "${cleandata}baseline_scores_standardized_grade_ms_5.dta", keep(1 3) gen(basesc_merge) keepusing(bl_*)

* Merge post enrollment scores
merge m:1 stu using "${cleandata}post_scores_standardized_grade_ms_6.dta", keep(1 3) gen(postsc_merge) keepusing(post_*)
destring stu, replace

// Keep only marginal students for the bandwidths
keep if marginal == 1

* Standardize all outcomes because we're comparing across bandwidths
* Note that we are not using any regents outcomes to calculate bandwidth
foreach test of varlist post_ss_math post_ss_ela post_ss_total bl_ss_math bl_ss_ela {

	egen mean_`test' = mean(`test')
	egen sd_`test' = sd(`test')
	gen ss_`test' = (`test' -  mean_`test') / sd_`test'

}

* Get higher order terms for baseline test scores
gen bl_ss_ela2  = bl_ss_ela^2
gen bl_ss_ela3  = bl_ss_ela^3
gen bl_ss_math2 = bl_ss_math^2
gen bl_ss_math3 = bl_ss_math^3

* Set outcomes to run for regular setup (6th grade ela and math scores)
local tests ss_post_ss_math ss_post_ss_ela

* Indicate kids who get an offer
 egen any_offer = max(offer), by(stu)
 su any_offer
 assert r(max) == 1 & r(min) == 0

* Indicator for having non-missing ranks
g has_runvar = .
replace has_runvar = 1 if lottery == 0
replace has_runvar = 0 if lottery == 0 & indi_missing_rank & inlist(programtype, "Screened", "Screened for Language")
replace has_runvar = 0 if lottery == 0 & indi_missing_compositescore & programtype == "False Test Outcome"
replace has_runvar = 0 if lottery == 0 & indi_missing_testoutcome & programtype == "Test Outcome"
replace has_runvar = 0 if lottery == 1

// Loop over outcomes
foreach test of varlist `tests'  {

	* generate bandwidth vars
	gen ccftcovs_bwall_`test' = .

	* generate bandwidths
	su non_lotto_programcode
	forval i = 1/`r(max)' {

		di in red "doing program `i' "

		* Create bandwidth for applicants who are applying to a non lotto program
		* that is fully ranked and only look at the marginal applicants
		noi cap: rdbwselect `test' hs_rank_centered if non_lotto_programcode == `i' & fully_ranked_prog == 1 & marginal == 1 & has_runvar, ///
			covs(bl_swd bl_female bl_asian bl_black bl_hispanic bl_white bl_fr_lunch bl_ss_ela bl_ss_ela2 bl_ss_ela3 bl_ss_math bl_ss_math2 bl_ss_math3) kernel(uniform)
		if _rc ==0 replace ccftcovs_bwall_`test' = e(h_mserd) if non_lotto_programcode == `i'

	}

}

* Generate minimum bandwidth across the different outcomes for each non lotto program
foreach x in bwall {

	if `grad_sep' == 0 {
		egen ccftcovs_`x'	  	= rowmin(ccftcovs_`x'_ss_post_ss_math ccftcovs_`x'_ss_post_ss_ela)
		egen gradccftcovs_`x'	= rowmin(ccftcovs_`x'_ss_post_ss_math ccftcovs_`x'_ss_post_ss_ela)
	}
	if `grad_sep' == 1 {
		egen ccftcovs_`x'	  	= rowmin(ccftcovs_`x'_ss_sat_math ccftcovs_`x'_ss_sat_ela)
		egen gradccftcovs_`x'  	= rowmin(ccftcovs_`x'_ss_grad_ever)
	}

}

// Keep program ID and the bandwidths
keep non_lotto_programcode programcode *_bwall

// Make unique
duplicates drop
isid programcode

// save
sa "${cleandata}bw_for_schools_min_ms_${year}_`grad_sep'`suffix'.dta", replace
